package get_ctx_value

import (
	"github.com/gin-gonic/gin"
	"github.com/redis/go-redis/v9"
	"gorm.io/gorm"
	"huijing_mall/internal/mq"
	"huijing_mall/shared/kernel/hmErrors"
	"huijing_mall/variable"
)

type DBS struct {
	DB  *gorm.DB
	Rdb *redis.Client
	Mq  *mq.RabbitMQ
}

// 防止丢失
var DBMap map[int]*DBS

func GetDBS(c *gin.Context) DBS {
	dbs := c.MustGet(variable.DBS).(*DBS)
	if dbs == nil { //为空 直接取map
		return *DBMap[0]
	}
	return *dbs
}

func GetUID(c *gin.Context) uint {
	uid := c.MustGet(variable.UID).(uint)
	if uid == 0 { //为空 直接 panic
		panic(hmErrors.ErrSystem("程序内部异常，获取UID错误"))
	}
	return uid
}
